// pages/submit_exams/index.js
import { IndexModel } from "../../models/index.js";
const indexModel = new IndexModel();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        titleMsg:'',
        courseId:0,
        mockId:0,
        answerRecordData:null,
        submitDialog:false,
        doCount:0,
        noDoCount:0,
        doRightCount:0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    async onLoad(options) {
        this.setData({
            titleMsg:options.courseName,
            courseId:options.courseId,
            mockId:options.mockId
        });
        //获取答题情况(章节练习和历年真题练习才有答题记录)
        let records = await indexModel.answerRecords(8,options.mockId);
        this.setData({
           answerRecordData:records.topicList,
           doCount:records.doCount,
           noDoCount:records.noDoCount,
           doRightCount:records.doRightCount
       });
       if(options.submitType==1){
           //时间到了强制交卷的情况
           this.doSubmitExam();
       }
    },
    selNumberTopic(event){
        let indexNum=event.currentTarget.dataset.index;
        let curIndex=indexNum%10;
        let topicIndex=(curIndex?(curIndex-1):(indexNum-1));//计算在分页的情况下是第几个
        //计算是第几页
        let clickPage=Math.ceil(indexNum/10);
        //选择题目跳转过去
        let pages=getCurrentPages();
        let prePage=pages[pages.length-2];
        prePage.setData({
            page:clickPage,
            courseId:this.data.courseId,
            mockId:this.data.mockId,
            currentIndex:topicIndex,
            returnShowDialog:false,
            isSubmitBack:1
        });
        wx.navigateBack({
            delta:1
       });
    },
    //交卷
    submitExam(){
        this.setData({
            submitDialog:true
        });
    },
    //继续答题
    continueExam(){
        this.setData({
            submitDialog:false
        });
    },
    //确认交卷
    async doSubmitExam(){
        //记录分数
        await indexModel.examDone(this.data.doRightCount,this.data.mockId,this.data.courseId);
        let topicCount=this.data.answerRecordData.length;
        const app = getApp();
        app.globalData.mockUnload = true;
        wx.reLaunch({
            url: `/pages/exam_result/index?mockId=${this.data.mockId}&courseId=${this.data.courseId}&topicCount=${topicCount}&rightNum=${this.data.doRightCount}`,
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})